/**
 * iSocial Project
 * http://isocial.missouri.edu
 *
 * Copyright (c) 2011, University of Missouri iSocial Project, All Rights Reserved
 *
 * Redistributions in source code form must reproduce the above
 * copyright and this condition.
 *
 * The contents of this file are subject to the GNU General Public
 * License, Version 2 (the "License"); you may not use this file
 * except in compliance with the License. A copy of the License is
 * available at http://www.opensource.org/licenses/gpl-license.php.
 *
 * The iSocial project designates this particular file as
 * subject to the "Classpath" exception as provided by the iSocial
 * project in the License file that accompanied this code.
 */
package org.jdesktop.wonderland.modules.isocial.tokensheet.client.legacy;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.io.IOException;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.jdesktop.wonderland.client.hud.CompassLayout.Layout;
import org.jdesktop.wonderland.client.hud.HUD;
import org.jdesktop.wonderland.client.hud.HUDComponent;
import org.jdesktop.wonderland.modules.isocial.client.HUDDetailsWrapper;
import org.jdesktop.wonderland.modules.isocial.client.ISocialManager;
import org.jdesktop.wonderland.modules.isocial.client.view.ResultListener;
import org.jdesktop.wonderland.modules.isocial.client.view.SheetView;
import org.jdesktop.wonderland.modules.isocial.client.view.annotation.View;
import org.jdesktop.wonderland.modules.isocial.common.model.Result;
import org.jdesktop.wonderland.modules.isocial.common.model.Role;
import org.jdesktop.wonderland.modules.isocial.common.model.Sheet;
import org.jdesktop.wonderland.modules.isocial.tokensheet.common.TokenResult;
import org.jdesktop.wonderland.modules.isocial.tokensheet.common.TokenSheet;

/**
 *
 * @author Kaustubh
 */
//@View(value = TokenSheet.class, roles = {Role.STUDENT})
public class AllPassStrikeView extends JPanel implements SheetView, ResultListener {

    private ISocialManager manager;
    private Sheet sheet;
    private int rows;
    //private String url = "/org/jdesktop/wonderland/modules/isocial/tokensheet/client/resources/pass_strike.png";
    private String url = "/org/jdesktop/wonderland/modules/isocial/tokensheet/client/resources/NewPassStrike.png";
    private HUDComponent hudComponent;
    private HashMap<String, PassStrikeStudentPanel> allPanels;
    private HashMap<String, JLabel> allLabels;

    /** Creates new form AllPassStrikeView */
    public AllPassStrikeView() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setOpaque(false);
        setSize(new java.awt.Dimension(400, 300));
        setLayout(new java.awt.GridLayout(0, 2));
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    public void initialize(ISocialManager manager, Sheet sheet, Role role) {
        this.manager = manager;
        this.sheet = sheet;
        allPanels = new HashMap<String, PassStrikeStudentPanel>();
        allLabels = new HashMap<String, JLabel>();
        manager.addResultListener(sheet.getId(), this);
        try {
            if (manager.getResults(sheet.getId()).size() < 1) {
                this.setPreferredSize(new Dimension(334, 1));
            }
        } catch (IOException ex) {
            Logger.getLogger(AllPassStrikeView.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            ArrayList<Result> results = (ArrayList<Result>) manager.getResults(sheet.getId());
            Collections.sort(results, new Comparator<Result>() {

                public int compare(Result r1, Result r2) {
                    String creator1 = r1.getCreator().toLowerCase();
                    String creator2 = r2.getCreator().toLowerCase();
                    return Collator.getInstance().compare(creator1, creator2);
                }
            });
            for (Result r : results) {
                String studentName = r.getCreator();
                PassStrikeStudentPanel panel = new PassStrikeStudentPanel(url);
                panel.updateStudentStrikesPasses((TokenResult) r.getDetails());
                rows++;
                GridLayout gl = (GridLayout) getLayout();
                gl.setRows(rows);
                JLabel label = new JLabel();
                label.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 14));
                label.setHorizontalTextPosition(JLabel.LEFT);
                label.setText(studentName);
                int stringWidth = label.getFontMetrics(label.getFont()).stringWidth(studentName);
                JLabel pictureLabel = new JLabel(panel.getImageIcon());
                this.add(pictureLabel);
                this.add(label);
                this.setPreferredSize(new Dimension(panel.getImageIcon().getIconWidth() * 2,
                        panel.getImageIcon().getIconHeight() * rows));
                if (!allPanels.containsKey(studentName)) {
                    allPanels.put(studentName, panel);
                }
                if (!allLabels.containsKey(studentName)) {
                    allLabels.put(studentName, pictureLabel);
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(AllPassStrikeView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getMenuName() {
        return "All Passes Strikes View";
    }

    public boolean isAutoOpen() {
        return false;
    }

    public HUDDetailsWrapper open(HUD hud) {
        hudComponent = hud.createComponent(this);
        hudComponent.setPreferredLocation(Layout.EAST);
        hudComponent.setTransparency(1.0f);
        
        
         return new HUDDetailsWrapper(sheet.getName(), hudComponent, this);
    }
    

    public void close() {
        manager.removeResultListener(sheet.getId(), this);
    }

    public void resultAdded(Result result) {
        if (allPanels.containsKey(result.getCreator())) {
            PassStrikeStudentPanel panel = allPanels.get(result.getCreator());
            panel.resetImage();
            panel.updateStudentStrikesPasses((TokenResult) result.getDetails());
        }

        if (allLabels.containsKey(manager.getUsername())) {
            allLabels.get(manager.getUsername()).repaint();
        }
    }

    public void resultUpdated(Result result) {
        if (allPanels.containsKey(result.getCreator())) {
            PassStrikeStudentPanel panel = allPanels.get(result.getCreator());
            panel.resetImage();
            panel.updateStudentStrikesPasses((TokenResult) result.getDetails());
        }

        if (allLabels.containsKey(result.getCreator())) {
            allLabels.get(result.getCreator()).repaint();
        }
    }
}
